#include "TestProj.h"

TestProj::TestProj(string path1 , bool DMRW , bool Klien , bool Shasha_Zhang)
{
	string timesFileName,resultsFileName;
	if(path1[path1.length()-1] != '\\')
		path1 += '\\';
	path = path1;
	timesFileName = path+"times.txt";
	resultsFileName = path+"results.txt";
	result.open(resultsFileName.c_str());
	rtime.open(timesFileName.c_str());
	ChooseAlgorithm[0] = DMRW;
	ChooseAlgorithm[1] = Klien;
	ChooseAlgorithm[2] = Shasha_Zhang;
}

void TestProj::TestProjCreate(int rangeLimit ,Distance* distance)
{
	time_t start,end;
	Tree* F;
	Tree* G;
	string filename(path+"test");
	RTG generator(rangeLimit);
	//create a head start for the headline because of "i) "
	result<<"   ";
	rtime<<"   ";
	cout<<"Start test!"<<endl;//--------------------------------------------------
	for(int j=0; j<3; j++)
	{
		if(ChooseAlgorithm[j] && j==0 )
		{
			result<<"DMRW|";
			rtime<<"DMRW|";
		}
		if(ChooseAlgorithm[j] && j==1 )
		{
			result<<"Klien|";
			rtime<<"Klien|";
		}
		if(ChooseAlgorithm[j] && j==2 )
		{
			result<<"Shasha & Zhang";
			rtime<<"Shasha & Zhang";
		}
	}
	result<<endl;
	rtime<<endl;
	result<<"--------------------------------------------------------------------------------"<<endl;
	rtime<<"--------------------------------------------------------------------------------"<<endl;
	for(int i=0; i<rangeLimit ; i++)
	{
		string cfilenameF; //current filename for tree F
		string cfilenameG; //current filename for tree G
		int numOfDigits(0),temp=i;
		cfilenameG = cfilenameF = filename;
		//count the number of digits
		while(temp>0)
		{
			numOfDigits++;
			temp/=10;
		}
		//attach the current index to the filename
		if(numOfDigits == 0)
			cfilenameF = cfilenameG += '0';
		while(numOfDigits>0)
		{
			cfilenameF = cfilenameG += char('0' + getDigit(i,numOfDigits-1));
			numOfDigits--;
		}
		generator.Clear();
		generator.Generate();
		F = generator.getRoot();
		cfilenameF += "F.txt";
		F->Save(cfilenameF);
		generator.Clear();
		generator.Generate();
		G = generator.getRoot();
		cfilenameG += "G.txt";
		G->Save(cfilenameG);
		result<<i<<") ";
		rtime<<i<<") ";
		for(int j=0; j<3; j++)
		{
			if(ChooseAlgorithm[j])//check if the algorithm is included in the test
			{
				cout<<"Run test "<<i<<" algorithm"<<j<<endl;//--------------------------------------------------
				time(&start);
				TED test(F, G, distance, (Algorithm)i);
				time(&end);
				cout<<"The result is: "<<test.GetTED()<<endl;//--------------------------------------------------
				result<<test.GetTED()<<" ";
				rtime<<difftime(end,start)<<" ";
			}
		}
		result<<endl;
		rtime<<endl;
		//cout<<i<<endl;
		F->~Tree();
		G->~Tree();
	}
}

void TestProj::TestProjLoad(int amount ,Distance *distance)
{
	time_t start,end;
	Tree* F;
	Tree* G;
	string filename(path+"test");
	//create a head start for the headline because of "i) "
	result<<"   ";
	rtime<<"   ";
	for(int j=0; j<3; j++)
	{
		if(ChooseAlgorithm[j] && j==0 )
		{
			result<<"DMRW|";
			rtime<<"DMRW|";
		}
		if(ChooseAlgorithm[j] && j==1 )
		{
			result<<"Klien|";
			rtime<<"Klien|";
		}
		if(ChooseAlgorithm[j] && j==2 )
		{
			result<<"Shasha & Zhang";
			rtime<<"Shasha & Zhang";
		}
	}
	result<<endl;
	rtime<<endl;
	result<<"--------------------------------------------------------------------------------"<<endl;
	rtime<<"--------------------------------------------------------------------------------"<<endl;
	for(int i=0; i<amount ; i++)
	{
		string cfilenameF; //current filename for tree F
		string cfilenameG; //current filename for tree G
		int numOfDigits(0),temp=i;
		cfilenameG = cfilenameF = filename;
		//count the number of digits
		while(temp>0)
		{
			numOfDigits++;
			temp/=10;
		}
		//attach the current index to the filename
		if(numOfDigits == 0)
			cfilenameF = cfilenameG += '0';
		while(numOfDigits>0)
		{
			cfilenameF = cfilenameG += char('0' + getDigit(i,numOfDigits-1));
			numOfDigits--;
		}
		cfilenameF += "F.txt";
		cfilenameG += "G.txt";
		F = Load(cfilenameF);
		G = Load(cfilenameG);
		result<<i<<") ";
		rtime<<i<<") ";
		for(int j=0; j<3; j++)
		{
			if(ChooseAlgorithm[j])//check if the algorithm is included in the test
			{
				time(&start);
				TED test(F, G, distance, (Algorithm)i);
				time(&end);
				result<< test.GetTED()<<" ";
				rtime<<difftime(end,start)<<" ";
			}
		}
		result<<endl;
		rtime<<endl;
	//	cout<<i<<endl;
		F->~Tree();
		G->~Tree();
	}
}

int getDigit(int num, int index)
{
	while(index>0)
	{
		num/=10;
		index--;
	}
	return num%10;
}
